<?php
namespace app\admin\controller;
use app\admin\controller\Auth;
use think\Db;
use think\Session;
use think\Config;
class Index extends Auth{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$adminlist = Db::name('user_admin')->paginate(10);
		$this->assign('list',$adminlist);
		return $this->fetch();
	}
	// 渲染添加管理员
	public function addadmin(){
		return $this->fetch();
	}
	// 添加管理员操作
	public function doaddadmin(){
		$username = trim(input('post.username'));
		$pwd = trim(input('post.pwd'));
		if(empty($username) || empty($pwd)){
			$this->error('请输入用户名或密码');
			exit;
		}
		if(strlen($pwd) < 6 || strlen($pwd) > 16){
			$this->error('密码长度在6~16位之间');
			exit;
		}
		$data['username'] = $username;
		$data['pwd'] = md5($pwd);
		$data['createtime'] = time();
		$result = Db::name('user_admin')->insert($data);
		if($result){
			$this->redirect('/admin/index/index');
		}else{
			$this->error('添加失败');
		}
	}
	// 站点信息
	public function systemInfo(){
		$info = file_get_contents(ROOT_PATH.'public/system.txt');
		$info_arr = json_decode($info,true);
		$this->assign('info',$info_arr);
		return $this->fetch();
	}
	// 修改站点信息
	public function updateInfo(){
		$info = input('post.');
		foreach ($info as $key => $value) {
			if(empty($value)){
				$this->error('请将信息补充完整');
				exit;
			}
		}
		$data = json_encode($info);
		$result = file_put_contents(ROOT_PATH.'public/system.txt', $data);
		if($result){
			$this->redirect('/admin/index/systemInfo');
		}else{
			$this->error('修改失败');
		}
	}
	// 计算两个经纬度坐标之间的距离
	// lng1  经度
	// lat1  纬度
	public function getdistance($lng1 = '116.498448', $lat1 = '39.984323', $lng2 = '116.348646', $lat2 = '40.034290',$len_type = 1, $decimal = 2) {
	    $radLat1 = $lat1 * 3.1415926535898 / 180.0;
	    $radLat2 = $lat2 * 3.1415926535898 / 180.0;
	    $a = $radLat1 - $radLat2;
	    $b = ($lng1 * 3.1415926535898 / 180.0) - ($lng2 * 3.1415926535898 / 180.0);
	    $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
	    $s = $s * 6378.137;
	    $s = round($s * 1000);
	    if ($len_type > 1){
	    	$s /= 1000;
	    }
    	return round($s, $decimal);
	}
}